new Vue({
    el:"#app",
    data:{
        letter:['a','b','c','d','e','f','g','h'],
        number:[8,7,6,5,4,3,2,1],
        list:[],//bg,color,code,icon
        //          1 2 3  4 5 6 
        //按重要性： 兵马象 车后王
        icon:[
            "",
            ["","♙","♘","♗","♖",'♕','♔'],
            ["","♟","♞",'♝','♜','♛','♚'],
        ],  
        ready_i:-1,
        can_run:[],
        color:1,//1白棋走，2黑棋走
        status:0,
        msg:"",
        mode:1,//1 人机对战，2人人对战
        auto_timer:0,
        step:[],//每一步记录list和color,list不能是引用类型
        from:-1,
        to:-1,//上一步走的from/to
    },
    created(){
        this.init();
        var cache=fun.local("chess_cache");
        if(cache){
            for(var i in cache){
                this[i]=cache[i];
            }
        }
    },
    methods:{
        start(){
            this.status=1;
            this.ready_i=-1;
            this.can_run=[];
            this.color=1//1白棋走，2黑棋走
            this.msg=""
            this.now=-1//最后走的棋子
            this.step=[];
            this.init();
            //自动走
            this.ai_run(1);
        },
        cache_now(){
            fun.local("chess_cache",this.$data);
        },
        clear_cache(){
            fun.local('chess_cache',null);
            location.reload();
        },
        ai_run(color){
            if(this.mode==2) return;
            var re=chess.ai_run(this.list,color);
            this.do_run(re[0],re[1]);
        },
        auto(){
            if(this.status!=1){
                this.start();
            }
            this.mode=1;
            clearInterval(this.auto_timer);
            this.auto_timer=setInterval(()=>{
                if(this.status==1&&this.color==2){
                    this.ai_run(2);
                }
                if(this.status!=1){
                    clearInterval(this.auto_timer);
                }
            },1000)
        },
        stop_auto(){
            clearInterval(this.auto_timer);
        },
        init(){ 
           this.list=chess.init();
        },
        run(i,v){
            if(v.color==this.color){
                this.ready(i);
            }else if(this.ready_i>-1&&v.color!=this.color){
                this.do_run(this.ready_i,i);
            }
        },
        ready(i){
            var v=this.list[i];
            if(!v.icon||this.status!=1) return;
            this.ready_i=i;
            this.can_run=chess.get_can_run(this.list,i);
        },
        do_run(from,to){
            var can_run=chess.get_can_run(this.list,from);
            if(!fun.in_array(to,can_run)){
                return;
            }
            var item=this.list[from];//要移动的棋子
            //记录历史
            if(!item){
                console.log("error:",this.list,from,to);
            }
            this.step.push({
                color:this.color,
                list:fun.copy(this.list),
                from,
                to,
                code:item.code,
            })
            this.list=chess.move(this.list,from,to);
            this.color=this.color==1?2:1;
            this.ready_i=-1;
            this.can_run=[];
            this.from=from;
            this.to=to;
            this.check_status();
            this.cache_now();//缓存最后一步
        },
        prev_step(){
            var step=this.mode==1?2:1;
            for(var i=0;i<step;i++){
                var item=this.step[this.step.length-1];
                this.list=item.list;
                this.color=item.color;
                this.step.pop();
                this.now=-1;
                this.ready_i=-1;
                this.can_run=[];
            }
        },
        check_status(){
           var status=chess.check_status(this.list,this.color);
           if(status=='finish'){
               this.finish();
           }else if(status=="checking"){
               this.msg="将军";
               console.log("color",this.color);
           }else{
               this.msg="";
           }
           if(this.status==1&&this.color==1){
               //BUG:有一步没有自动走
               setTimeout(()=>{
                   this.ai_run(1);
               },500)
           }
        },
        finish(){
            this.status=2;
            this.msg=(this.color==1?'黑':'白')+"棋胜";
            setTimeout(()=>{
                alert("游戏结束");
            })
        },
      
    }
})

